<?php

namespace app\proxyapi\controller;

use app\data\model\LiveAppproxy;
use app\data\model\LiveProxyList;

class UserMy extends BaseProxyController
{
    //获取更新
    public function getUpdate(){
        $userId = $this->checkTokenReturnUserId();
        $post = $this->request->post();
        if(!isset( $post['appName'], $post['packageName'], $post['versionName'], $post['versionCode'])) $this->error('缺少参数！');
        if(empty($post['appName']) || empty($post['packageName']) || empty($post['versionName']) || empty($post['versionCode'])) $this->error('参数不可为空！');

        $app = LiveAppproxy::mk()->field('version_code,version_message,version_url')->where(['app_name'=>$post['appName'],'app_package'=>$post['packageName'],'deleted'=>0])->find();
        if(empty($app)) $this->error('不存在当前应用！');
        if($app['version_code'] <= $post['versionCode']){
            $this->success('已是最新版本！');
        }else{
            $this->success('更新！',$app,200);
        }
    }
    //修改密码
    public function updatePassword(){
        $post = $this->request->post();
        $userId = $this->checkTokenReturnUserId();
        if (!isset($post['new_user_password'], $post['old_user_password'])) $this->error('缺少参数');
        $user = LiveProxyList::mk()->where(['id' => $userId])->find();
        if (empty($user)) $this->error('不存在该用户！');
        if ($user['user_password'] != $post['old_user_password']) $this->error('旧密码不正确！');

        //更改用户
        LiveProxyList::mk()->where(['id' => $user['id']])->update([
            'user_password' => $post['new_user_password'],
        ]);
        $this->success('改密成功');
    }
    //修改昵称
    public function updateNickname(){
        $post = $this->request->post();
        $userId = $this->checkTokenReturnUserId();
        if (!isset($post['new_nickname'])) $this->error('缺少参数');
        $user = LiveProxyList::mk()->where(['id' => $userId])->find();
        if (empty($user)) $this->error('不存在该用户！');

        //更改用户
        LiveProxyList::mk()->where(['id' => $user['id']])->update([
            'user_nickname' => $post['new_nickname'],
        ]);
        $this->success('更改成功');
    }
    //获取账户余额
    public function getInteYue(){
        $post = $this->request->post();
        $userId = $this->checkTokenReturnUserId();
        $user = LiveProxyList::mk()->where(['id' => $userId])->find();
        if (empty($user)) $this->error('不存在该用户！');

        $this->success('success',$user['integral_balance']);
    }
}